
#include <stdio.h>

#define MAX_N 505

int g_arr[MAX_N][MAX_N]; 
int g_n, g_m; 

void dump_arr(void)
{
	int i, j; 
	for(i=1; i<=g_n; i++) {
		for (j=1; j<=g_n; j++) {
			printf("%d ", g_arr[i][j]); 
		}
		printf("\n"); 
	}
}

void init_arr(void)
{
	int	i, j; 
	int k=1; 
	for(i=1; i<=g_n; i++) {
		for (j=1; j<=g_n; j++) {
			g_arr[i][j] = k; 
			k++;
		}
	}
}

void rotate(int xo, int yo, int r, int anticlockwise)
{
	static int tmp[MAX_N][MAX_N]; 
	int	x1, y1, x2, y2, i1, j1, i2, j2; 
	
	for (x1=-r; x1<=r; x1++) {
		for (y1=-r; y1<=r; y1++) {
			i1 = x1 + xo; 
			j1 = y1 + yo; 
			if (anticlockwise) {
				y2 = x1; 
				x2 = -y1; 
			} else {
				y2 = -x1; 
				x2 = y1; 
			}
			i2 = x2 + xo; 
			j2 = y2 + yo; 
			tmp[i2][j2] = g_arr[i1][j1]; 
		}
	}
	
	for (x1=-r; x1<=r; x1++) {
		for (y1=-r; y1<=r; y1++) {
			i1 = x1 + xo; 
			j1 = y1 + yo; 
			g_arr[i1][j1] = tmp[i1][j1]; 
		}
	}		
}

int main()
{
	int	i, xo, yo, r, anticlockwise; 
	scanf("%d %d", &g_n, &g_m); 
	init_arr(); 
	for (i=0; i<g_m; i++) {
		scanf("%d %d %d %d", &xo, &yo, &r, &anticlockwise); 
		rotate(xo, yo, r, anticlockwise); 
	}
	dump_arr(); 
	return 0; 
}

